# consumers.py
import json
import time

from channels.exceptions import StopConsumer
from channels.generic.websocket import WebsocketConsumer
from django_q.tasks import result

from django_cli import settings


class GetAsyncStatus(WebsocketConsumer):
    def __init__(self, *args, **kwargs):
        super(GetAsyncStatus, self).__init__(*args, **kwargs)
        self.is_connected = False

    def websocket_connect(self, message):

        """
        客户端来向后端发送请求的时候 自动触发
        :param message:
        :return:
        """
        self.accept()
        self.is_connected = True
        print("Connect Success")

    def websocket_receive(self, message):
        """
        浏览器基于ws向后端发送数据，自动触发接受消息
        :param message:
        :return:
        """
        async_id = message.get("text")
        print("获取异步消息任务")

    def websocket_disconnect(self, message):
        """
        断开链接的时候触发
        :param message:
        :return:
        """
        self.is_connected = False
        raise StopConsumer()
